﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Moop.Common.IFaces.Database;
using Moop.Common;
using Moop.DataLayer.Olap.Impl;


namespace Moop.UI.Converters
{
    public class FolderConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //get folder _name listing...
            string folder = parameter as string ?? "";
            var folders = folder.Split(',').Select(f => f.Trim()).ToList();            
            while (values.Length > folders.Count) folders.Add(String.Empty);//...and make sure there are no missing entries            
            List<object> items = new List<object>();//this is the collection that gets all top level _items

            for (int i = 0; i < values.Length; i++)
            {                
                IEnumerable childs = values[i] as IEnumerable ?? new List<object> { values[i] }; //make sure were working with collections from here...
                string folderName = folders[i];
                var entities = childs.Cast<IOlapEntity>();

                if (folderName != String.Empty && entities.Count() > 0)
                {
                    FolderItem folderItem = new FolderItem { Name = folderName, Items = entities };//create folder item and assign childs
                    Type firstEntityType = entities.Take(1).FirstOrDefault().GetType();

                    if (firstEntityType != null && firstEntityType == typeof(CubeDimensionHierarchy))
                    {
                        foreach (ICubeDimensionHierarchy hier in entities)
                        {
                            if (hier.Levels.Count > 0)
                            {
                                /*
                                FolderItem levelsFolderItem = new FolderItem { Name = "Levels", SubFolderItems = hier.Levels };
                                folderItem.SubFolderItems = new FolderSubItem { Name = "Levels", Items = hier.Levels };
                                var its = new List<IOlapEntity>(folderItem.Items.ToList()); //-> Add levels to Hierarchies
                                its.AddRange(hier.Levels);
                                */
                                folderItem.SubFolderItems = new FolderSubItem { Name = "Levels", Items = hier.Levels };
                            }
                        }                        
                    }
                    items.Add(folderItem);
                }
                else
                {                    
                    foreach (var child in childs) { items.Add(child); }//if no folder _name was specified, move the item directly to the root item
                }
            }
            return items;
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("Cannot perform reverse-conversion");
        }
    }
}
